package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.system.domain.Precords;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Grecords;
import com.ruoyi.system.service.IGrecordsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 剩余车位Controller
 *
 * @author ruoyi
 * @date 2023-10-20
 */
@RestController
@RequestMapping("/system/grecords")
public class GrecordsController extends BaseController
{
    @Autowired
    private IGrecordsService grecordsService;

    /**
     * 查询剩余车位列表
     */
    @PreAuthorize("@ss.hasPermi('system:grecords:list')")
    @GetMapping("/list")
    public TableDataInfo list(Grecords grecords)
    {
        startPage();
        QueryWrapper<Grecords> queryWrapper=new QueryWrapper<>();
        if(grecords.getGaragenum()!=null&&!"".equals(grecords.getGaragenum())){
            queryWrapper.like("garagenum",grecords.getGaragenum());
        }
        List<Grecords> list = grecordsService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 获取停车场信息概览
     */
    @GetMapping("/getParkingInfo")
    public AjaxResult getParkingInfo()
    {
        // 查询所有车位记录
        List<Grecords> allGrecords = grecordsService.list();
        
        // 计算总车位、已使用车位和剩余车位
        int totalSpaces = 0;
        int spareSpaces = 0;
        
        for (Grecords grecord : allGrecords) {
            if (grecord.getTotalplace() != null) {
                totalSpaces += grecord.getTotalplace();
            }
            if (grecord.getSpareplace() != null) {
                spareSpaces += grecord.getSpareplace();
            }
        }
        
        int usedSpaces = totalSpaces - spareSpaces;
        
        // 构建返回数据
        ParkingInfoDTO parkingInfo = new ParkingInfoDTO();
        parkingInfo.setTotalSpaces(totalSpaces);
        parkingInfo.setUsedSpaces(usedSpaces);
        parkingInfo.setRemainingSpaces(spareSpaces);
        
        // 计算使用率
        if (totalSpaces > 0) {
            parkingInfo.setUsageRate((double) Math.round((usedSpaces * 100.0 / totalSpaces)));
        } else {
            parkingInfo.setUsageRate(0.0);
        }
        
        return success(parkingInfo);
    }
    
    // 停车场信息DTO
    public static class ParkingInfoDTO {
        private int totalSpaces;     // 总车位数
        private int usedSpaces;      // 已使用车位
        private int remainingSpaces; // 剩余车位
        private double usageRate;    // 使用率
        
        // getter和setter方法
        public int getTotalSpaces() {
            return totalSpaces;
        }
        
        public void setTotalSpaces(int totalSpaces) {
            this.totalSpaces = totalSpaces;
        }
        
        public int getUsedSpaces() {
            return usedSpaces;
        }
        
        public void setUsedSpaces(int usedSpaces) {
            this.usedSpaces = usedSpaces;
        }
        
        public int getRemainingSpaces() {
            return remainingSpaces;
        }
        
        public void setRemainingSpaces(int remainingSpaces) {
            this.remainingSpaces = remainingSpaces;
        }
        
        public double getUsageRate() {
            return usageRate;
        }
        
        public void setUsageRate(double usageRate) {
            this.usageRate = usageRate;
        }
    }

    /**
     * 获取剩余车位详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:grecords:query')")
    @GetMapping(value = "/{gid}")
    public AjaxResult getInfo(@PathVariable("gid") Long gid)
    {
        return success(grecordsService.getById(gid));
    }

    /**
     * 新增剩余车位
     *//*
    @PreAuthorize("@ss.hasPermi('system:grecords:add')")
    @Log(title = "剩余车位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Grecords grecords)
    {
        return toAjax(grecordsService.insertGrecords(grecords));
    }

    *//**
     * 修改剩余车位
     *//*
    @PreAuthorize("@ss.hasPermi('system:grecords:edit')")
    @Log(title = "剩余车位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Grecords grecords)
    {
        return toAjax(grecordsService.updateGrecords(grecords));
    }

    *//**
     * 删除剩余车位
     *//*
    @PreAuthorize("@ss.hasPermi('system:grecords:remove')")
    @Log(title = "剩余车位", businessType = BusinessType.DELETE)
    @DeleteMapping("/{gids}")
    public AjaxResult remove(@PathVariable Long[] gids)
    {
        return toAjax(grecordsService.deleteGrecordsByGids(gids));
    }*/
}
